# -*- coding: utf-8 -*-

from sklearn.metrics import accuracy_score
from sklearn import svm
import numpy as np

class Second_Layer_SVM:
    
    def __init__(self,stacking,embedding,method='on'):
        order_inv = stacking.order_inv
        Train_Second_X = stacking.Train_Second_X
        Test_Second_X = stacking.Test_Second_X
        Train_Second_Y = stacking.Train_Second_Y
        Test_Second_Y = stacking.Test_Second_Y
        Train_Second_Title_distance = embedding.Train_Second_Title_distance
        Test_Second_Title_distance = embedding.Test_Second_Title_distance
        if method == 'on':
            Train_Second_X = np.concatenate((Train_Second_X, Train_Second_Title_distance), axis=1)
            Test_Second_X = np.concatenate((Test_Second_X, Test_Second_Title_distance), axis=1)
        self.predictions = Second_Layer_SVM.SVM(Train_Second_X,Test_Second_X,Train_Second_Y,Test_Second_Y,order_inv)
        
    @staticmethod
    def label2word(Test_prediction,order):
        Origin = []
        for i in Test_prediction:
            Origin.append(order[i])
        return Origin
    
    @staticmethod
    def SVM(Train_X,Test_X,Train_Y,Test_Y,order):
        lin_clf = svm.LinearSVC(C=0.1,random_state=0)
        lin_clf.fit(Train_X,Train_Y) 
        Train_prediction = lin_clf.predict(Train_X)
        print ('SVM : Traning Set Accuracy: %f' % accuracy_score(Train_Y,Train_prediction))

        Test_prediction = lin_clf.predict(Test_X)
        print ('SVM : Test Set Accuracy: %f' % accuracy_score(Test_Y,Test_prediction))
        
        predictions = Second_Layer_SVM.label2word(Test_prediction,order)
        return predictions